//
//  ViewController.m
//  JKArkanoidGame
//
//  Created by 王冲 on 2018/4/24.
//  Copyright © 2018年 希艾欧科技有限公司. All rights reserved.
//

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#define JKRGBColor(r,g,b,c) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:c]
//屏幕尺寸
#define JK_SCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define JK_SCREEN_HEIGHT  [UIScreen mainScreen].bounds.size.height

@interface ViewController ()
{
    //用这个布尔值标示游戏是否正在进行中
    BOOL isPlaying;
    
    //游戏时钟
    CADisplayLink *_gameTimer;
    
    //小球的移动速度
    CGPoint _speed;
    
    //手指移动位置的差值
    CGFloat chaX;
    
}


@property(nonatomic,strong) NSMutableArray *blockImageArray;

@property(nonatomic,strong) UIImageView *ballImage;

@property(nonatomic,strong) UIImageView *paddleImage;

//检测屏幕碰撞
- (BOOL)checkWithScreen;

//检测砖块碰撞
-(BOOL)checkWithBlock;

//游戏结束处理事件
- (void)gameOver:(NSString *)str;

//挡板碰撞
- (void)checkWithPaddle;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = JKRGBColor(187, 187, 187, 1);
    
    self.blockImageArray = [[NSMutableArray alloc]init];
    
    for (int i = 0; i< 20; i++) {
        
        UIImageView *imageView = [[UIImageView alloc]init];
        
        if (i < 5) {
            
            imageView.frame = CGRectMake(i*(JK_SCREEN_WIDTH/5.0), 0, JK_SCREEN_WIDTH/5.0, 20);
            imageView.image = [UIImage imageNamed:@"blue"];
            
            
        }else if ( i>4 && i<10){
            
            imageView.frame = CGRectMake((i-5)*(JK_SCREEN_WIDTH/5.0), 20, JK_SCREEN_WIDTH/5.0, 20);
            imageView.image = [UIImage imageNamed:@"green"];
            
        }else if ( i>9 && i<15){
            
            imageView.frame = CGRectMake((i-10)*(JK_SCREEN_WIDTH/5.0), 40, JK_SCREEN_WIDTH/5.0, 20);
            imageView.image = [UIImage imageNamed:@"red"];
            
        }else{
            
            imageView.frame = CGRectMake((i-15)*(JK_SCREEN_WIDTH/5.0), 60, JK_SCREEN_WIDTH/5.0, 20);
            imageView.image = [UIImage imageNamed:@"yellow"];
        }
        
        [self.blockImageArray addObject:imageView];
        [self.view addSubview:imageView];
        
    }

    [self.view addSubview:self.ballImage];
    [self.view addSubview:self.paddleImage];
    
    
    
}

//点击屏幕开始游戏
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //游戏是否在进行中，如果没开始就让游戏开始
    if(!isPlaying){
        
        isPlaying = YES;
        
        //初始化一个游戏始终,让step方法1/60秒执行一次
        _gameTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(step)];
        
        
        
        //将游戏时钟放在runloop中
        /*
         * runLoop做两件事情
         
         1、监听输入源，一般自动放到runloop中
         2、监听定时器，一般需手动放到runloop中
         */
        [_gameTimer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        
        
        //给小球一个初始速度
        _speed = CGPointMake(0, -5);
        
        
    }else{
        
        chaX = 0;
        
    }
    
    
}



//让小球按照1/60秒的频率动起来
- (void)step{
    
    //如果返回yes就代表游戏失败
    if([self checkWithScreen]){
        
        [self gameOver:@"再来一次"];
    }
    
    //如果返回yes，就代表游戏过关
    if ([self checkWithBlock]) {
        
        [self gameOver:@"你真棒"];
    }
    
    [self checkWithPaddle];
    
    //小球每次的移动都是以上一次的位置为参照
    _ballImage.center = CGPointMake(_ballImage.center.x+_speed.x, _ballImage.center.y+_speed.y);
}


#pragma mark 碰撞检测

- (BOOL)checkWithScreen{
    
    BOOL isFail = NO;
    
    //CGRectGetMinY 获取到小球的最小y值
    //顶部碰撞
    if (CGRectGetMinY(_ballImage.frame) <= 0) {
        
        //fabsf获取到绝对值
        _speed.y = fabs(_speed.y);
        
    }
    
    //底部碰撞
    if (CGRectGetMaxY(_ballImage.frame) >= CGRectGetHeight(self.view.frame)) {
        
        isFail = YES;
        
    }
    
    //左侧碰撞
    if(CGRectGetMinX(_ballImage.frame) <= 0){
        
        _speed.x = fabs(_speed.x);
    }
    
    //右侧碰撞
    if(CGRectGetMaxX(_ballImage.frame) >= CGRectGetWidth(self.view.frame)){
        
        _speed.x = -1 * fabs(_speed.x);
    }
    
    
    
    return isFail;
    
}

- (void)gameOver:(NSString *)str{
    
    isPlaying = NO;
    
    NSLog(@"%@",str);
    
    //销毁定时器
    [_gameTimer invalidate];
    
    for (UIImageView *block in _blockImageArray) {
        
        block.hidden = YES;
        
        block.alpha = 0;
    }
    
    [UIView animateWithDuration:2.0 animations:^{
        
        for (UIImageView *block in _blockImageArray) {
            
            block.hidden = NO;
            
            block.alpha = 1;
        }
        
    } completion:^(BOOL finished) {
        
        
        _ballImage.frame = CGRectMake(JK_SCREEN_WIDTH/2.0-10, JK_SCREEN_HEIGHT-175, 20, 20);
        _paddleImage.frame = CGRectMake(JK_SCREEN_WIDTH/2.0-60, JK_SCREEN_HEIGHT-150, 120, 20);
        
        
    }];
    
    
}

//砖块碰撞
- (BOOL)checkWithBlock{
    
    BOOL gameWin = YES;
    
    //通过for循环找到小球跟哪个色块碰撞了
    for (UIImageView *block in _blockImageArray) {
        
        //CGRectIntersectsRect判断两个试图是否碰撞
        if (CGRectIntersectsRect(block.frame, _ballImage.frame) && !block.hidden) {
            
            block.hidden = YES;
            
            _speed.y = fabs(_speed.y);
            
        }
        
        
    }
    
    
    //用来判断是否所有的色块都隐藏了
    for (UIImageView *block in _blockImageArray) {
        
        if (block.hidden == NO) {
            
            gameWin = NO;
            
            break;
            
        }
        
    }
    
    return gameWin;
    
}

//挡板碰撞
- (void)checkWithPaddle{
    
    if (CGRectIntersectsRect(_ballImage.frame, _paddleImage.frame)) {
        
        
        _speed.y = -1 * fabs(_speed.y);
        
        //让小球的x方向的速度跟手指瞬时移动的差值相加
        _speed.x += chaX;
        
        NSLog(@"speed.x=%f",_speed.x);
    }
    
}

//监听在屏幕上的滑动
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    if(isPlaying){
        
        //获取我点击的手指，touches是个集合，因为试图有多点触摸
        UITouch *touch = [touches anyObject];
        
        //获取到当前手指x的值
        CGFloat nowX = [touch locationInView:self.view].x;
        
        //获取到上一次手指x的值
        CGFloat lastX = [touch previousLocationInView:self.view].x;
        
        chaX = nowX-lastX;
        
        //让挡板移动
        _paddleImage.center = CGPointMake(_paddleImage.center.x+chaX, _paddleImage.center.y);
        
    }
   
}

-(UIImageView *)ballImage{
    
    if (!_ballImage) {
        
        _ballImage = [[UIImageView alloc]initWithFrame:CGRectMake(JK_SCREEN_WIDTH/2.0-10, JK_SCREEN_HEIGHT-175, 20, 20)];
        _ballImage.layer.cornerRadius = 10;
        _ballImage.clipsToBounds = YES;
        _ballImage.image = [UIImage imageNamed:@"ball"];
    }
    return _ballImage;
}

-(UIImageView *)paddleImage{
    
    if (!_paddleImage) {
        
        _paddleImage = [[UIImageView alloc]initWithFrame:CGRectMake(JK_SCREEN_WIDTH/2.0-60, JK_SCREEN_HEIGHT-150, 120, 20)];
        _paddleImage.layer.cornerRadius = 10;
        _paddleImage.clipsToBounds = YES;
        _paddleImage.image = [UIImage imageNamed:@"paddle"];
    }
    return _paddleImage;
}

@end

